package org.plex.core.imp;

import java.util.Map;

import org.plex.core.FormManager;
import org.plex.form.FormServiceInvokeResponse;
import org.plex.form.imp.FormServiceInvokeResponseImp;
import org.plex.task.FormInstance;
import org.plex.task.imp.FormInstanceImp;

import com.adobe.idp.Document;
import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;
import com.adobe.idp.taskmanager.dsc.client.TaskManagerClientFactory;
import com.adobe.idp.taskmanager.dsc.client.task.TaskManager;
import com.adobe.idp.taskmanager.dsc.client.task.TaskManagerException;

public class FormManagerImp implements FormManager
{
	private ServiceClientFactory	serviceClientFactory;

	private TaskManager				taskManager;

	public FormManagerImp(ServiceClientFactory factory)
			throws NullPointerException
	{
		if (null == factory)
		{
			throw new NullPointerException();
		}
		this.setServiceClientFactory(factory);
		taskManager = TaskManagerClientFactory.getTaskManager(factory);
	}

	public FormServiceInvokeResponse renderForm(long taskId,
			long formInstanceId, Map inputMap)
	{
		// TODO Auto-generated method stub
		try
		{
			FormServiceInvokeResponseImp imp = new FormServiceInvokeResponseImp(
					taskManager.callRenderService(taskId, formInstanceId,
							inputMap));
			return imp;
		} catch (NullPointerException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (TaskManagerException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public void setServiceClientFactory(
			ServiceClientFactory serviceClientFactory)
	{
		this.serviceClientFactory = serviceClientFactory;
		taskManager = TaskManagerClientFactory
				.getTaskManager(serviceClientFactory);
	}

	public ServiceClientFactory getServiceClientFactory()
	{
		return serviceClientFactory;
	}

	public FormInstance getEmpytFormInstance()
	{
		// TODO Auto-generated method stub
		return new FormInstanceImp(taskManager.getEmptyForm());
	}

	@Deprecated
	public FormInstance getFormInstanceForTask(long taskId)
			throws NullPointerException, TaskManagerException
	{
		// TODO Auto-generated method stub
		return new FormInstanceImp(taskManager.getFormInstanceForTask(taskId));
	}

	@Deprecated
	public FormInstance getFormInstanceForTask(long taskId, boolean retrieveData)
			throws NullPointerException, TaskManagerException
	{
		// TODO Auto-generated method stub
		return new FormInstanceImp(taskManager.getFormInstanceForTask(taskId,
				retrieveData));
	}

	public FormInstance getFromInstanceForTask(long taskId, long formInstanceId)
			throws NullPointerException, TaskManagerException
	{
		// TODO Auto-generated method stub
		return new FormInstanceImp(taskManager.getFormInstanceForTask(taskId,
				formInstanceId));
	}

	public FormInstance getFromInstanceForTask(long taskId,
			long formInstanceId, boolean retrieveData)
			throws NullPointerException, TaskManagerException
	{
		return new FormInstanceImp(taskManager.getFormInstanceForTask(taskId,
				formInstanceId, retrieveData));
	}

	public FormServiceInvokeResponse submitForm(long taskId, long formId,
			Document documentSubmitted, Map inputMap) throws NullPointerException, TaskManagerException
	{
		// TODO Auto-generated method stub
		return new FormServiceInvokeResponseImp(taskManager.callSubmitService(taskId, formId,documentSubmitted, inputMap));
	}

}
